package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.vo.OrderVo;
import com.ruoyi.system.service.IGoodsService;
import com.ruoyi.system.service.IOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OrderDetails;
import com.ruoyi.system.service.IOrderDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单详情Controller
 *
 * @author ruoyi
 * @date 2023-12-23
 */
@RestController
@RequestMapping("/system/orderdetails")
public class OrderDetailsController extends BaseController {
    @Autowired
    private IOrderDetailsService orderDetailsService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderDetails orderDetails) {
        startPage();
        List<OrderDetails> list = orderDetailsService.selectOrderDetailsList(orderDetails);
        return getDataTable(list);
    }

    /**
     * 导出订单详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetails:export')")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderDetails orderDetails) {
        List<OrderDetails> list = orderDetailsService.selectOrderDetailsList(orderDetails);
        ExcelUtil<OrderDetails> util = new ExcelUtil<OrderDetails>(OrderDetails.class);
        util.exportExcel(response, list, "订单详情数据");
    }

    /**
     * 获取订单详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderDetailsID}")
    public AjaxResult getInfo(@PathVariable("orderDetailsID") Long orderDetailsID) {
        return success(orderDetailsService.selectOrderDetailsByOrderDetailsID(orderDetailsID));
    }

    /**
     * 新增订单详情
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderVo datas) {
        int k = 0;
        BigDecimal price = BigDecimal.valueOf(0);
        Long orderDetailsID = datas.getOrderID();
        Map<Long, Long> data = datas.getData();
        for (Long goodID : data.keySet()) {
            Goods goods = goodsService.selectGoodsByGoodID(goodID);
            BigDecimal sellPrice = goods.getSellPrice();// 获取商品售价
            price = price.add(sellPrice.multiply(BigDecimal.valueOf(data.get(goodID))));// 添加价格
            goods.setNum(goods.getNum() - data.get(goodID));    // 减去商品数量
            goodsService.updateGoods(goods);    // 更新
            k = orderDetailsService.insertOrderDetails(new OrderDetails(orderDetailsID, goodID, data.get(goodID)));
        }
        if (k > 0) {
            Order order = orderService.selectOrderByOrderID(orderDetailsID);
            order.setAllPrice(price);
            return toAjax(orderService.updateOrder(order));
        }
        return toAjax(0);
    }

    /**
     * 修改订单详情
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetails:edit')")
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderDetails orderDetails) {
        return toAjax(orderDetailsService.updateOrderDetails(orderDetails));
    }

    /**
     * 删除订单详情
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetails:remove')")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderDetailsIDs}")
    public AjaxResult remove(@PathVariable Long[] orderDetailsIDs) {
        return toAjax(orderDetailsService.deleteOrderDetailsByOrderDetailsIDs(orderDetailsIDs));
    }
}
